package com.lon.over.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.lon.over.R;
import com.lon.over.model.domain.HomePagerContent;
import com.lon.over.utils.LogUtils;
import com.lon.over.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * author: ctl | 陈泰龙
 * 创建时间: 2022/4/25
 * Describe:
 */
public class HomePageContentAdapter extends RecyclerView.Adapter<HomePageContentAdapter.HolderAdapter> {

    private List<HomePagerContent.DataBean> mData = new ArrayList<>();

    @NonNull
    @Override
    public HolderAdapter onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_pager_content, parent, false);
        return new HolderAdapter(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull HolderAdapter holder, int position) {
        HomePagerContent.DataBean dataBean = mData.get(position);
        holder.setDAta(dataBean);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(List<HomePagerContent.DataBean> content) {
        mData.clear();
        mData.addAll(content);
        notifyDataSetChanged();
    }

    public void addContentData(List<HomePagerContent.DataBean> content) {
        int oldSize = mData.size();
        mData.addAll(content);
        notifyItemRangeChanged(oldSize, content.size());
    }

    static class HolderAdapter extends RecyclerView.ViewHolder {
        ImageView cover;
        TextView title, offPrice, price, originalPrice, goodsBuyPeople;

        HolderAdapter(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            cover = itemView.findViewById(R.id.goods_img);
            title = itemView.findViewById(R.id.goods_title);
            offPrice = itemView.findViewById(R.id.goods_off_price);
            price = itemView.findViewById(R.id.goods_price);
            originalPrice = itemView.findViewById(R.id.goods_original_price);
            goodsBuyPeople = itemView.findViewById(R.id.goods_buy_people);
        }

        void setDAta(HomePagerContent.DataBean dataBean) {
            Context context = itemView.getContext();
            long couponAmount = dataBean.getCoupon_amount(); // 优惠价
            String finalPrice = dataBean.getZk_final_price(); //原价
            float resultPrice = Float.parseFloat(finalPrice) - couponAmount; // 券后价
            long volume = dataBean.getVolume(); // 购买人数
            //减少图片大小，减少流量使用
            ViewGroup.LayoutParams params = cover.getLayoutParams();
            int height = params.height;
            int width = params.width;
            int coverSize = (Math.max(width, height)) / 2;
            String pathCover = UrlUtils.getPathCover(dataBean.getPict_url(), coverSize);
            //设置显示数据
            Glide.with(context).load(pathCover).into(cover);
            title.setText(dataBean.getTitle());
            offPrice.setText(String.format(context.getString(R.string.text_goods_off_price), couponAmount));
            price.setText(String.format("%.2f", resultPrice));
            originalPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            originalPrice.setText(String.format(context.getString(R.string.text_goods_original_price), finalPrice));
            goodsBuyPeople.setText(String.format(context.getString(R.string.text_goods_volume), volume));
        }
    }
}
